package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.Collect;
import cn.cnf.entity.User;
import cn.cnf.service.CollectionService;
import cn.cnf.service.MoneyRecordService;
import cn.cnf.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.istack.internal.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 服务控制器
 *
 * @author ZhuMeiYi
 * @description
 * @since 2020-12-03 12:47:52
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MoneyRecordService moneyRecordService;
    @Autowired
    private CollectionService collectionService;

    @GetMapping("/info")
    public Result queryMyself(String username) {

        try {
            return Result.success(userService.findUserByUsername(StringUtils.isBlank(username) ? UserRequestUtil.getCurrentUserName() : username));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        try {
            user.setUsername(UserRequestUtil.getCurrentUserName());
            return Result.success(userService.updateByUsername(user));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/moneyUpdate")
    public Result moneyUpdate(BigDecimal num) {
        try {
            return Result.success(moneyRecordService.adjustUserMoney(UserRequestUtil.getCurrentUserName(), num, false));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 打赏作品
     *
     * @param workId
     * @param type
     * @param money
     * @return
     */
    @GetMapping("/rewards")
    public Result rewardsWork(String workId, Integer type, BigDecimal money) {
        try {
            return Result.success(moneyRecordService.rewardsWork(UserRequestUtil.getCurrentUserName(), workId, WorkType.getByValue(type), money));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @PostMapping("/collect")
    public Result addCollection(@RequestBody Collect collect) {
        try {
            return Result.success(collectionService.addCollection(collect));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/cancelCollect")
    public Result delCollection(@RequestParam("workId")String workId,@RequestParam("workType")Integer workType) {
        try {
            return Result.success(collectionService.delCollection(workId,WorkType.getByValue(workType)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/personalComicCollect")
    public Result personalComicCollect(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(collectionService.personalComicCollect(current,pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/personalNovelCollect")
    public Result personalNovelCollect(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(collectionService.personalNovelCollect(current,pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    //密码校验
    @GetMapping("/checkPwd")
    //pwd:原密码   id：用户id
    public Result checkPwd(@NotNull String pwd,@NotNull String id){
        try {
            return Result.success(userService.checkPwd(pwd,id));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }


    //修改密码
    @PostMapping("/changePwd")
    //传用户ID：id   新密码：password
    public Result changePwd(@RequestBody User user){
        try {
            LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(User::getId,user.getId());
            User user1 = userService.getOne(wrapper);
            if(!user1.getPassword().equals(user.getPassword())){
                return Result.success(userService.changePwd(user));
            }else{
                return Result.error("新密码和旧密码相同，请重新输入");
            }
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }


    @GetMapping("/allUser")
    public Result getAllUser(String name,Integer current,Integer pageSize){
        try {
            Page<User> iPage = new Page<>();
            IPage page = userService.getAllUser(name,current,pageSize,iPage);
            System.out.println(page.getRecords());
            return Result.success(page);
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }


}